package com.example.hy2110szyx.controller;

import com.example.hy2110szyx.common.DatatablesJSON;
import com.example.hy2110szyx.common.MyJsonResult;
import com.example.hy2110szyx.entity.*;

import com.example.hy2110szyx.service.CustomerService;
import com.example.hy2110szyx.service.ReservedService;
import com.example.hy2110szyx.vo.PageVo;
import com.example.hy2110szyx.vo.ReservedVo;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/reservation", produces = "application/json")
@Slf4j
@CrossOrigin(origins = "*")
public class ReservedController {

    @Autowired
    private ReservedService reservedService;
    @Autowired
    private CustomerService customerService;

    public ReservedController(ReservedService reservedService) {
        this.reservedService = reservedService;
    }


    @GetMapping("/query")
    @ResponseBody
    public DatatablesJSON<ReservedTicket> query(
            int draw,
            @RequestParam("order[0][column]") int orderColumn,
            @RequestParam("order[0][dir]") String orderDir,
            int start, int length, String identity, String name, Integer gender
    ) {
        String[] columns = {null, "tidentity", "tname", "cgender", "departureTime", "toCity", "fno", "price", null};
        ReservedVo reservedConn = new ReservedVo(identity, name, gender);
        log.debug("order[0][column]=" + orderColumn);
        log.debug("order[0][dir]=" + orderDir);
        log.debug("reservedConn is {}", reservedConn);
        PageVo<ReservedVo> pageVo = new PageVo(start, length, columns[orderColumn], orderDir, reservedConn);
        Page<ReservedTicket> page = reservedService.queryAllReservedForPage(pageVo);
        DatatablesJSON<ReservedTicket> datatablesJSON = new DatatablesJSON<>();
        datatablesJSON.setDraw(draw);
        datatablesJSON.setRecordsFiltered(page.getTotal());
        datatablesJSON.setData(page.getResult());
        return datatablesJSON;

    }

    /**
     * 添加订单
     *
     * @param reserved
     */
    @PostMapping("/add")
    @ResponseStatus(HttpStatus.CREATED)
    public MyJsonResult addOne(@RequestBody ReservedVo reserved) {
        log.info("数据：-----{}",reserved);
        String identity = reserved.getIdentity();//用户身份证
        ReservedVo ticket  = new ReservedVo();
        ticket.setIdentity(identity);
        List<ReservedVo> ticketList = reservedService.queryAllConn(ticket);

        if(ticketList.size() > 0){// todo 有此人订单，比对是否重复购票
            for (ReservedVo reservedTicket : ticketList) {
                log.info("到这里-----{}", reserved);
                 if(reservedTicket.getDepartureTime().compareTo(reserved.getDepartureTime()) == 0){//如果购票时间重复，result+1
                     return new MyJsonResult(1,"添加失败，您已经有同时间的机票，不需要重复预定", null,null);
                 }
            }
        }
        // todo 没有此身份证订单，可以买
        if(customerService.queryByIdentity(identity) == null){
            //todo 添加新用户
            reservedService.insertOne(new ReservedTicket(reserved.getIdentity(),reserved.getName(),reserved.getFno(),reserved.getCno()));
            reservedService.insertOneCustomer(new Customer(reserved.getIdentity(),reserved.getGender(),reserved.getName()));
            reservedService.insertOneFlightInfo(new FlightInfo(reserved.getFno(),reserved.getDepartureTime(),reserved.getRno()));
            reservedService.insertOneRoute(new Route(reserved.getToCity(),reserved.getRno()));
            reservedService.insertOneDistribution(new Distribution(reserved.getPrice(),reserved.getCno(),reserved.getFno()));
        }else {//只添加航班订单信息
            reservedService.insertOne(new ReservedTicket(reserved.getIdentity(),reserved.getName(),reserved.getFno(),reserved.getCno()));
//            reservedService.insertOneCustomer(new Customer(reserved.getIdentity(),reserved.getGender()));
            reservedService.insertOneFlightInfo(new FlightInfo(reserved.getFno(),reserved.getDepartureTime(),reserved.getRno()));
            reservedService.insertOneRoute(new Route(reserved.getToCity(),reserved.getRno()));
            reservedService.insertOneDistribution(new Distribution(reserved.getPrice(),reserved.getCno(),reserved.getFno()));
        }
        return new MyJsonResult(0, "添加成功", null, reserved);

    }

    @DeleteMapping("/delsome")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult delSome(@RequestBody Integer id,String identity,String fno,String rno ) {
        reservedService.delOneCustomer(identity);
        reservedService.delOneFlightInfo(fno);
        reservedService.delOneRoute(rno);
        reservedService.delOneDistribution(fno);
        reservedService.deleteSomeById(id);
        return new MyJsonResult(0, "删除成功", null, null);
    }
}
